
package com.example.a16478095zxl;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    ProgressBar progressBar;
    ImageButton start, stop;
    DownService.MyBinder binder;
    ImageView imageView_photo,imageView_photo2,imageView_photo3,imageView_photo4;
    ProgressDialog dialog=null;
    ServiceConnection sc = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder = (DownService.MyBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @SuppressLint("HandlerLeak")
    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Log.d("myapp", "get image...");
                    imageView_photo.setImageBitmap((Bitmap) msg.obj);

                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
    }
    @Override
    public void onClick(View view) {
        switch(view.getId()) {
            case R.id.btn_start:
                dialog = ProgressDialog.show(MainActivity.this, "资源加载中",
                        "下载数据，请稍等 …", true, true);
                break;
        }
    }
    private void initEvent() {
        Intent in = new Intent(this, DownService.class);
        in.putExtra("messenger",new Messenger(handler));
        startService(in);
        bindService(in, sc, BIND_AUTO_CREATE);

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressBar.setVisibility(View.VISIBLE);
                binder.startDown();
                progressBar.setVisibility(View.GONE);
            }



        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binder.stopDown();
            }
        });
    }

    private void initView() {
        imageView_photo = findViewById(R.id.image_photo);
        imageView_photo2 = findViewById(R.id.image_photo2);
        imageView_photo3 = findViewById(R.id.image_photo3);
        imageView_photo4 = findViewById(R.id.image_photo4);
        start = findViewById(R.id.btn_start);
        stop = findViewById(R.id.btn_stop);
        progressBar=findViewById(R.id.progress);
        progressBar.setVisibility(View.GONE);
    }


}
